import com.ysre.sheep.Sheep;
import com.ysre.task.CallableHunt;
import com.ysre.task.HuntResult;
import com.ysre.wolf.Wolf;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class RoundRobin {

    static String[] name1 = {"灰太狼","红太狼","蕉太狼","西瓜太郎"};
    static String[] name2 = {"喜羊羊","美羊羊","沸羊羊","懒洋洋","老村长"};

//    private static ExecutorService executor = Executors.newFixedThreadPool(5);
//    private static ExecutorService executor = Executors.newCachedThreadPool();
    private static ThreadPoolExecutor executor = new ThreadPoolExecutor(0,10,
        10,TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(1000));
    private static AtomicInteger count = new AtomicInteger(0);

    /**
     * 做一个灰太狼抓捕喜羊羊的故事
     * 必须的要素:狼,羊,
     * Future对象(用来表示抓羊的执行结果)
     * FutureTask 获取任务结果
     * ExecutorService 执行任务
     * @param args
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //每一只狼都去抓羊,知道没有狼,或者没有羊
        Queue<Wolf> wolfQueue = new ConcurrentLinkedDeque<>(getWolves(name1));
        Queue<Sheep> sheepQueue = new ConcurrentLinkedDeque<>(getSheep(name2));
        boolean endFlag = wolfQueue.isEmpty() || sheepQueue.isEmpty();
        while (!endFlag){
            List<Future<HuntResult>> result = startRound(wolfQueue,sheepQueue);
            //展示结果
            for(Future<HuntResult> fs : result){
                HuntResult s = fs.get();
                System.out.println(s.desc);
                if(s.success){
                    wolfQueue.offer(s.wolf);
                }else {
                    sheepQueue.offer(s.sheep);
                }
            }
            endFlag = wolfQueue.isEmpty() || sheepQueue.isEmpty();
        }
        if(!wolfQueue.isEmpty()){
            System.out.println("获胜者是狼队" + wolfQueue);
        }
        if(!sheepQueue.isEmpty()){
            System.out.println("获胜者是羊队" + sheepQueue);
        }
    }

    private static List<Future<HuntResult>> startRound(Queue<Wolf> wolfQueue, Queue<Sheep> sheepQueue) throws ExecutionException, InterruptedException {
        List<Future<HuntResult>> futures = new ArrayList<>();
        boolean endFlag = wolfQueue.isEmpty() || sheepQueue.isEmpty();
        while (!endFlag){
            Wolf wolf = wolfQueue.poll();
            Sheep sheep = sheepQueue.poll();
            System.out.println(LocalTime.now() + "----round " + count.incrementAndGet() + wolf.getName() + "向" + sheep.getName() + "发起了攻击");
            futures.add(executor.submit(new CallableHunt(wolf,sheep)));


            endFlag = wolfQueue.isEmpty() || sheepQueue.isEmpty();
        }
        return futures;
    }


    private static List<Wolf> getWolves(String... names){
        List<Wolf> wolves = new ArrayList<>();
        if(names == null) return wolves;
        for(int i = 0; i< names.length; i++){
            wolves.add(new Wolf(names[i], (int) (Math.random() * 100)));//0~100
        }
        System.out.println("狼队战力" + wolves);
        return wolves;
    }

    private static List<Sheep> getSheep(String... names){
        List<Sheep> sheep = new ArrayList<>();
        if(names == null) return sheep;
        for(int i = 0 ;i< names.length; i++){
            sheep.add(new Sheep(names[i],(int) (Math.random() * 100)));
        }
        System.out.println("羊队战力" + sheep);
        return sheep;
    }


}
